import wepy from 'wepy';

import * as OauthApi from './OauthApi';



/**
 * @description 获取店铺小程序二维码
 * @param {String} shop_id 店铺序号
 */
export async function getCode(shop_id) {
    let config = wepy.$instance.globalData.globalConfig;

    try {
        let times = 0;
        if (arguments.length > 1) {
            times = arguments[1];
        }
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v2/code/getCode", {
            shop_id: shop_id
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return getCode(shop_id, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return getCode(shop_id, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}
/**
 * @description 发送短信验证码
 * @param {String} phone 手机号
 * @param {String} shopId 店铺id
 */
export async function sendCode(phone, shopId) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 2) {
            times = arguments[2];
        }
        let result = await OauthApi.postWithClient(config.BASE_URL + "/api/v2/code/send/SMS", {
            phone: phone,
            shopId: shopId
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return sendCode(phone, shopId, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetCredentialsToken();
                    return sendCode(phone, shopId, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 绑定手机号
 * @param {Number} phone 手机号
 * @param {Number} verfiyCode 验证码
 * @param {String} username  用户名
 * @param {String} shopId 店铺id 必传
 * @param {String} openid 用户openid 必传
 * @param {String} share_openid 推荐人openid
 */
export async function bingdingPhone(phone, verfiyCode, username, openid, shopId, share_openid) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 6) {
            times = arguments[6];
        }
        let result = await OauthApi.postWithClient(config.BASE_URL + "/api/v2/code/bingding/phone", {
            phone: phone,
            verfiyCode: verfiyCode,
            username: username,
            shopId: shopId,
            openid: openid,
            share_openid: share_openid
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return bingdingPhone(phone, verfiyCode, username, openid, shopId, share_openid, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return bingdingPhone(phone, verfiyCode, username, openid, shopId, share_openid, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}   